package com.atguigu.gmall.realtime.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
    public static JedisPool jedisPool = null;
    public static Jedis getJedis(){
        if(jedisPool == null){
            JedisPoolConfig  jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(100);// 最大可用链接数目
            jedisPoolConfig.setBlockWhenExhausted(true); //设置连接耗尽是否等待
            jedisPoolConfig.setMaxWaitMillis(2000); //设置等待时间

            jedisPoolConfig.setMaxIdle(5);
            jedisPoolConfig.setMinIdle(5);

            jedisPoolConfig.setTestOnBorrow(true); // 取连接时的时候运行一次测试 ping pong
            jedisPool = new JedisPool(jedisPoolConfig,"localhost",6379,1000);
            System.out.println("开始连接");
            return jedisPool.getResource();
        }else {
            System.out.println("连接池： "+ jedisPool.getNumActive());
            return jedisPool.getResource();
        }
    }

}
